package com.aopcinemas.common.ui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.aopcinemas.cinema.model.Schedule;

public class ScheduleTable extends JTable {

    private static final long serialVersionUID = 1L;

    private List<Schedule> schedules = new ArrayList<Schedule>();

    public ScheduleTable() {
        setModel(new ScheduleTableModel());
    }

    public List<Schedule> getSchedules() {
        return schedules;
    }

    public Schedule getSelectedSchedule() {
        int row = getSelectedRow();
        if (row < 0) {
            return null;
        }
        return schedules.get(row);
    }

    public void setSchedules(List<Schedule> schedules) {
        this.schedules = schedules;
    }

    private class ScheduleTableModel extends DefaultTableModel {

        private static final long serialVersionUID = 1L;

        private final String[] COLUMN_NAMES = new String[] { "Movie", "Hall", "Time", "Genre", "Age constraint", "Price" };

        @Override
        public String getColumnName(int column) {
            return COLUMN_NAMES[column];
        }

        @Override
        public int getColumnCount() {
            return 6;
        }

        @Override
        public Object getValueAt(int row, int column) {
            Schedule schedule = schedules.get(row);
            switch (column) {
                case 0: {
                    return schedule.getMovie().getName();
                }
                case 1: {
                    return schedule.getHall().getName();
                }
                case 2: {
                    return schedule.getDate();
                }
                case 3: {
                    return schedule.getMovie().getGenre();
                }
                case 4: {
                    return schedule.getMovie().getAgeConstraint();
                }
                case 5: {
                    return schedule.getBasePrice();
                }
            }
            return "Hei, yo!";
        }

        @Override
        public int getRowCount() {
            return schedules.size();
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    }

}
